package com.example.yan_music.widge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;

import com.example.yan_music.R;

/**
 * <P>
 *     自定义的加载框/对话框
 *     采用内部属性类构造（热干面构造模式，需要什么加什么）
 * </P>
 */
public class CustomLoadDialog extends Dialog {

    public CustomLoadDialog(@NonNull Context context) {
        super(context);
    }

    public CustomLoadDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    /**
     * 属性内部类
     */
    public static class Builder{
        private Context mContext;
        private boolean isCancelable; // 点击其他区域是否可以取消
        private boolean isCancelableOutside; // 点击物理按键是否可以取消
        private String mMessage; // 加载框里面的提示文字
        private int mDioLogStyle = R.style.custom_load_dialog; // 加载框的样式
        private int mDioLogLayoutRes; // 加载框的布局

        public Builder(Context context){
            this.mContext = context;
        }

        public Builder setIsCancelable(boolean cancelable) {
            isCancelable = cancelable;
            return this;
        }

        public Builder setIsCancelableOutside(boolean cancelableOutside) {
            isCancelableOutside = cancelableOutside;
            return this;
        }

        public Builder setMessage(String mMessage) {
            this.mMessage = mMessage;
            return this;
        }

        public Builder setLayoutRes(int layoutRes){
            this.mDioLogLayoutRes = layoutRes;
            return this;
        }

        public CustomLoadDialog create(){
            View view = null;
            LayoutInflater inflater = LayoutInflater.from(mContext);
            // 加载布局文件
            if(mDioLogLayoutRes != 0){
               view  = inflater.inflate(mDioLogLayoutRes, null);
            }
            CustomLoadDialog loadDialog = new CustomLoadDialog(mContext, mDioLogStyle);
            loadDialog.setContentView(view);
            loadDialog.setCancelable(isCancelable);
            loadDialog.setCanceledOnTouchOutside(isCancelableOutside);
            // 这里设置具体的宽高是因为自定义的在布局里设置无效
            loadDialog.getWindow().setLayout(350, 250);
            return loadDialog;
        }
    }

}
